#!/usr/bin/perl
use warnings;
use strict;

my $LOC = 0;
my $totalLOC = 0;
my $totalLines = 0;
my $numFiles = 0;

my @files = <../src/*>;
foreach my $file (@files) {
	if($file =~m/(.*\.java)$/) {
		my $fileName = $1;
		open('source', $file) or die("Error: cannot open file '$file'\n");
		$numFiles++;
		my $ignoreCommentBlock = 0;

		while( my $line = <source> ) {

			if($line =~ m/\/\*/) {
				$ignoreCommentBlock = 1;
			} elsif(!$ignoreCommentBlock && $line =~ m/[;\{:]/  && !($line =~ m/^\s*(\*|\/\/)/)) {
				$LOC++;
			}

			if($ignoreCommentBlock && $line =~ m/\*\//) {
				$ignoreCommentBlock = 0;
			}

			$totalLines++;
		}

		print "$fileName: $LOC\n";

		$totalLOC += $LOC;
		$LOC = 0;

		close('source');
	} else {
		my @tmp = <$file/*>;
		push(@files, @tmp);
	}
}

print "\nNumber of Files = $numFiles\n";
print "Total lines = $totalLines\n";
print "Total lines of code = $totalLOC\n";
my $avgLinesOfCode = int($totalLOC / $numFiles);
print "Average lines of code per file: $avgLinesOfCode\n";
exit(0);
